package com.bilibili.dubbo;

import com.bilibili.model.mongo.UserLike;
import com.bilibili.model.mongo.UserLocation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.NearQuery;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HuangRongFu
 * 2021/11/25 星期四4:40 下午
 */

@SpringBootTest
@RunWith(SpringRunner.class)
public class test {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void addLocation(){

        String[] addresses = {"深圳黑马程序员","红荔村肠粉","深圳南头直升机场","深圳市政府","欢乐谷",
                "世界之窗","东部华侨城","大梅沙海滨公园","深圳宝安国际机场","海雅缤纷城(宝安店)"};
        double[] y={22.582111,22.587995,22.562578,22.549528,22.547726,22.540746,22.632275,
                22.598196,22.638172,22.566223};
        double[] x={113.929778,113.925528,113.93814,114.064478,113.986074,113.979399,
                114.294924,114.314011,113.821705,113.912386};

        List<UserLocation> locationList = new ArrayList<>();
        long timeMillis = System.currentTimeMillis();
        for (int i = 0; i < addresses.length; i++) {

            UserLocation userLocation = new UserLocation();

            userLocation.setLocation(new GeoJsonPoint(x[i], y[i]));
            userLocation.setCreated(timeMillis);
            userLocation.setUpdated(timeMillis);
            userLocation.setAddress(addresses[i]);
            userLocation.setUserId(Long.valueOf(101 + i));

            locationList.add(userLocation);
        }

        mongoTemplate.insertAll(locationList);
    }


    //查询附近
    @Test
    public void testSearchNearBy(){

        Point point = new Point(113.929778 ,22.582111);//圆心
        Distance distance = new Distance(20, Metrics.KILOMETERS);//半径

        Circle circle = new Circle(point, distance);

        Query location = new Query(Criteria.where("location").withinSphere(circle));

        List<UserLocation> userLocations = mongoTemplate.find(location, UserLocation.class);

        userLocations.forEach(System.out::println);
    }

    //查询附近且获取间距
    @Test
    public void testNear1() {
        //1. 构造中心点(圆点)
        GeoJsonPoint point = new GeoJsonPoint(113.929778,22.582111);
        //2. 构建NearQuery对象 maxDistance最大距离， 在它以内的
        NearQuery query = NearQuery.near(point, Metrics.KILOMETERS).maxDistance(5, Metrics.KILOMETERS);
        //3. 调用mongoTemplate的geoNear方法查询
        GeoResults<UserLocation> results = mongoTemplate.geoNear(query, UserLocation.class);
        //4. 解析GeoResult对象，获取距离和数据
        for (GeoResult<UserLocation> result : results) {
            UserLocation location = result.getContent();
            double value = result.getDistance().getValue();
            System.out.println(location.getAddress()+"---距离："+value + "km");
        }
    }

    @Test
    public void Test(){

        long timeMillis = System.currentTimeMillis();

        for (int i = 90; i < 100; i++) {

            UserLike userLike = new UserLike();

            userLike.setUpdated(timeMillis);
            userLike.setCreated(timeMillis);
            userLike.setIsLike(true);
            userLike.setUserId(Long.valueOf(i));
            userLike.setLikeUserId(10001l);

            mongoTemplate.insert(userLike);
        }
    }

}
